<?php
namespace app\models;
use ac\component\tool\Tool;
use app\models\Basic;

use Yii;
use yii\helpers\Html;

class Goods extends Basic
{
    
    public static function tableName()
    {
        return '{{%pj8_goods}}';
    }

    public function rules()
    {
        return [
            ['order_id', 'integer', 'message' => 'order_id必须为数字，或数字字符串', 
                         'min'=>1, 'tooSmall' => 'order_id最小存储1'],
            ['member_id', 'integer', 'message' => 'member_id必须为数字，或数字字符串'],
            // ['catids', 'required', 'message' => 'catids不能为空', 'on' => 'add'],
            ['catids', 'string', 'message' => 'catids必须是字符串',
                                // 'min' => 2, 'tooShort' => 'catids最短2位',
                                'max' => 512, 'tooLong' => 'catids最长512位'],
            ['catid', 'required', 'message' => 'catid不能为空', 'on' => 'add'],
            ['catid', 'integer', 'message' => 'catid必须为数字，或数字字符串', 
                     'min'=>0, 'tooSmall' => 'catid最小存储0', 
                     'max'=>65535, 'tooBig' => 'catid最大存储65535'],
            ['name', 'required', 'message' => 'name不能为空', 'on' => 'add'],
            ['name', 'string', 'message' => 'name必须是字符串',
                                'min' => 1, 'tooShort' => 'name最短1位',
                                'max' => 18, 'tooLong' => 'name最长18位'],
            ['dynasty', 'string', 'message' => 'dynasty必须是字符串',
                                'max' => 18, 'tooLong' => 'dynasty最长18位'],
            ['long', 'string', 'message' => 'long必须是字符串',
                                'max' => 18, 'tooLong' => 'long最长18位'],
            ['wide', 'string', 'message' => 'wide必须是字符串',
                                'max' => 18, 'tooLong' => 'wide最长18位'],
            ['high', 'string', 'message' => 'high必须是字符串',
                                'max' => 18, 'tooLong' => 'high最长18位'],
            ['thick', 'string', 'message' => 'thick必须是字符串',
                                'max' => 18, 'tooLong' => 'thick最长18位'],
            ['diameter', 'string', 'message' => 'diameter必须是字符串',
                                'max' => 18, 'tooLong' => 'diameter最长18位'],
            ['weight', 'string', 'message' => 'weight必须是字符串',
                                'max' => 18, 'tooLong' => 'weight最长18位'],
            ['material', 'string', 'message' => 'material必须是字符串',
                                'max' => 18, 'tooLong' => 'material最长18位'],
            ['preview', 'default', 'value' => '', 'on' => 'add'],
            ['preview', 'string', 'message' => 'preview必须是字符串',
                                'min' => 2, 'tooShort' => 'preview最短2位',
                                'max' => 1024, 'tooLong' => 'preview最长1024位'],
            ['desc', 'default', 'value' => '', 'on' => 'add'],
            ['desc', 'string', 'message' => 'desc必须是字符串',
                                'min' => 2, 'tooShort' => 'desc最短2位',
                                'max' => 64, 'tooLong' => 'desc最长64位'],
            // ['phone', 'required', 'message' => 'phone不能为空', 'on' => 'add'],
            // ['phone', 'string', 'message' => 'phone必须是字符串',
            //                     'min' => 1, 'tooShort' => 'phone最短1位',
            //                     'max' => 18, 'tooLong' => 'phone最长18位'],
            // ['province', 'default', 'value' => ''],
            // ['province', 'string', 'message' => 'province必须是字符串', 
            //              'max' => 16, 'tooLong' => 'province最长16位'],
            // ['city', 'default', 'value' => ''],
            // ['city', 'string', 'message' => 'city必须是字符串', 
            //          'max' => 16, 'tooLong' => 'city最长16位'],
            // ['district', 'default', 'value' => ''],
            // ['district', 'string', 'message' => 'district必须是字符串', 
            //              'max' => 16, 'tooLong' => 'district最长16位'],
            // ['addr', 'default', 'value' => ''],
            // ['addr', 'string', 'message' => 'addr必须是字符串', 
            //              'max' => 128, 'tooLong' => 'addr最长128位'],

            // ['experts_id', 'string', 'message' => 'experts_id必须是字符串',
            //                     'max' => 4294967295, 'tooLong' => 'experts_id最长4294967295位'],
            // ['auth_type', 'integer', 'message' => 'auth_type必须为数字，或数字字符串'],
            // ['auth_type', 'in', 'range' => array(1, 2, 3), 'message' => 'auth_type值必须在1~3中选择'],

            // ['state', 'default', 'value' => 1, 'on' => 'add'],
            ['state', 'integer', 'message' => 'state必须为数字，或数字字符串'],
            ['state', 'in', 'range' => array(1, 2, 3, 4), 'message' => 'state值必须在1~4中选择'],

            ['fail_reason', 'default', 'value' => ''],
            ['fail_reason', 'string', 'message' => 'fail_reason必须是字符串', 
                         'max' => 64, 'tooLong' => 'fail_reason最长64位'],

            ['pass_time', 'default', 'value' => '1000-01-01 00:00:00', 'on' => 'add'],
            ['pass_time', 'date', 'format'=>'yyyy-MM-dd HH:mm:ss', 'message' => 'pass_time非yyyy-MM-dd HH:mm:ss格式'],
            ['add_time', 'default', 'value' => '1000-01-01 00:00:00', 'on' => 'add'],
            ['add_time', 'date', 'format'=>'yyyy-MM-dd HH:mm:ss', 'message' => 'add_time非yyyy-MM-dd HH:mm:ss格式'],
        ];
    }


    /*添加*/
    public function addGoods($data)
    {
        // P($data);changeCatidFormat
        $this->scenario = 'add';
        if(isset($data['Goods']['catids']) and is_array($data['Goods']['catids'])){
            $data['Goods']['catid'] = $data['Goods']['catids'][0]['catid'];
            $data['Goods']['catids'] = $this->changeCatidFormat($data['Goods']['catids']);
        }
        if(isset($data['Goods']['preview']) and is_array($data['Goods']['preview'])){
            $data['Goods']['preview'] = Tool::changeUploadFormat($data['Goods']['preview']);
        }

        if(!isset($data['Goods']['add_time']) or empty($data['Goods']['add_time'])){
            $data['Goods']['add_time'] = date('Y-m-d H:i:s', time());
        }
        // P($data);
        if($this->load($data) and $this->validate()){
            // P($this);
            if($this->save(false)){
                $id = $this->getPrimaryKey();
                return $id;
            }
        }
        return false;
    }

    /*
    修改
    $data       修改的数据
    */
    public function modifyGoods($data, $id)
    {
        // P($data);
        $goods = self::find()->where('goods_id = :id', [':id' => $id])->one();
        // P($goods);
        if(isset($data['Goods']['catids']) and is_array($data['Goods']['catids'])){
            $data['Goods']['catid'] = $data['Goods']['catids'][0]['catid'];
            $data['Goods']['catids'] = $this->changeCatidFormat($data['Goods']['catids']);
        }
        if(isset($data['Goods']['preview']) and is_array($data['Goods']['preview'])){
            $data['Goods']['preview'] = Tool::changeUploadFormat($data['Goods']['preview']);
        }
        // P($data);
        if(!is_null($goods)){
            if($this->load($data) and $this->validate()){
                // P($data);
                if(isset($this->catid)){
                    $goods->catid = $this->catid;
                }
                if(isset($this->catids)){
                    $goods->catids = $this->catids;
                }
                if(isset($this->name)){
                    $goods->name = $this->name;
                }
                if(isset($this->dynasty)){
                    $goods->dynasty = $this->dynasty;
                }
                if(isset($this->long)){
                    $goods->long = $this->long;
                }
                if(isset($this->wide)){
                    $goods->wide = $this->wide;
                }
                if(isset($this->high)){
                    $goods->high = $this->high;
                }
                if(isset($this->thick)){
                    $goods->thick = $this->thick;
                }
                if(isset($this->diameter)){
                    $goods->diameter = $this->diameter;
                }
                if(isset($this->weight)){
                    $goods->weight = $this->weight;
                }
                if(isset($this->material)){
                    $goods->material = $this->material;
                }
                if(isset($this->preview)){
                    $goods->preview = $this->preview;
                }
                if(isset($this->desc)){
                    $goods->desc = $this->desc;
                }
                if(isset($this->state)){
                    $goods->state = $this->state;
                }
                if(isset($this->fail_reason)){
                    $goods->fail_reason = $this->fail_reason;
                }
                if(isset($this->state) and $goods->state == 1){
                    $goods->pass_time = date('Y-m-d H:i:s', time());
                }
                // P($goods);
                if($goods->save(false)){
                    return true;
                }
            }
        };
        return false;
    }

    /*多个分类  把数组变成字符串*/
    private function changeCatidFormat($arr)
    {
        $res = [];
        if(is_array($arr)){
            for($i=0; $i<count($arr); $i++){
                $res[$i] = isset($arr[$i]['catid'])?$arr[$i]['catid']:"";
            }
        }
        if(!empty($res)){
            return implode(',', $res);
        }
        return $res;
    }


    /*获取内容的分类信息*/
    public function getCategory()
    {
        $info = $this->hasOne(GoodsCategory::className(), ['catid' => 'catid'])
                    ->select(['{{%pj8_goods_category}}.catid', 'alias']);
        return $info;
    }

    /*获取订单信息*/
    public function getOrder()
    {
        $info = $this->hasOne(OrderInfo::className(), ['order_id' => 'order_id'])
                    ->select(['member_id']);
        return $info;
    }
    
}